package com.wlts.wzy.Base;

import com.baomidou.mybatisplus.extension.service.IService;
import com.wlts.wzy.Result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Wuzhiyong
 * on 2019-02-22 11:29
 */
@RestController
public abstract class BaseController<T extends BaseEntity> {

    @Autowired
    private IService<T> baseService;

    public abstract IService<T> getBaseService();
    @ApiOperation(value = "增加")
    @RequestMapping(value = "/addEntity", method = RequestMethod.POST)
    public Result<T> addEntity(@RequestBody T t
    ) {
        baseService.save(t);
        return Result.buildResult(t);
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delById", method = RequestMethod.POST)
    public Result<T> delById(@RequestBody BaseIdParam t
    ) {

        T t1 = baseService.getById(t.getId());

        if (t1 != null) {
            t1.setDelFlag("1");
            baseService.updateById(t1);
            return new Result<>();
        }
        return new Result<>(500,"id = " + t.getId() + " 不存在");
    }

    @ApiOperation(value = "修改")
    @RequestMapping(value = "/chaEntity", method = RequestMethod.POST)
    public Result<T> chaEntity(@RequestBody T t
    ) {
        baseService.updateById(t);
        return Result.buildResult(t);
    }

    @ApiOperation(value = "id查询")
    @RequestMapping(value = "/getById", method = RequestMethod.POST)

    public Result<T> getById(@RequestBody BaseIdParam t
    ) {
        return Result.buildResult(baseService.getById(t.getId()));
    }

//    @ApiOperation(value = "分页")
//    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
//    public Result<IPage<T>> getPage(@RequestBody T t
//    ) {
//        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
//        IPage<T> p=  baseService.page(new Page<T>(t.getCurrentPage(),t.getPageSize()), queryWrapper);
//          return Result.buildResult(p);
//    }

}
